package com.qq.supplier.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qq.supplier.entity.Car;
import com.qq.supplier.entity.CarIn;
import com.qq.supplier.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/car")
public class CarController {

    @Autowired
    CarService carService;

    //查询 进货页面 所有车辆
    @RequestMapping("/carShow")
    String queryAll(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Car> cars = carService.queryAll();
        PageInfo<Car> carsPageInfo = new PageInfo<>(cars);
        model.addAttribute("PageInfo1", carsPageInfo);
        return "supplier/car/showcar";
    }

    //添加车辆信息
    @RequestMapping("/addPag")
    String addPag() {
        return "supplier/car/add";
    }

    @RequestMapping("/add")
    String add(Car car) {
        CarIn temp = carService.select(car);//从数据库查出来的记录
        int count;
        if (temp != null) {
            car.setCarInDate(new Date());
            count = temp.getCarTotals();//获取库存页面的数量
            count += car.getCarTotal();//传进来的记录--对应的数量
            temp.setCarTotals(count);//将值赋给数据库中查出来的记录
            carService.addCar(car);
            temp.setCarInDates(new Date());
            carService.editCarIn(temp);
            return "redirect:carSystem";
        } else {
            car.setCarInDate(new Date());
            carService.addCarIn(car);
            carService.addCar(car);
            return "redirect:carSystem";
        }

       /* int total;
        for (int i = 0; i < cars.size(); i++) {
           if(cars.get(i).getCarVar().equals(car.getCarVar())&&cars.get(i).getCarName().equals(car.getCarName())&&cars.get(i).getCarVersion().equals(car.getCarVersion())&&cars.get(i).getCarColor().equals(car.getCarColor())&&cars.get(i).getCarAddress().equals(car.getCarAddress())&&cars.get(i).getCarSource().equals(car.getCarSource())){
                    car.setCarInDate(new Date());
                    total=cars.get(i).getCarTotal();
                    total+=car.getCarTotal();
                   // carService.addCar(car);
                    return "supplier/car/carSystem";
                }else {
                    carIn.setCarInDates(new Date());
                    carService.addCarIn(carIn);
                    return "redirect:carShow";
                }
            }*/

    }

    //编辑库存页车辆信息---只改变数量
    @RequestMapping("/editPag/{id}")
    String editPag(@PathVariable String id, Model model) {
        model.addAttribute("cars", carService.queryOne(id));
        return "supplier/car/edit";
    }

    @RequestMapping("/edit")
    String edit(CarIn carIn) {
        carIn.setCarInDates(new Date());
        carService.editCarIn(carIn);
        return "redirect:carShow";
    }

    //车辆主页---显示库存
    @RequestMapping("/carSystem")
    String selects(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<CarIn> list = carService.querys();
        PageInfo<CarIn> pageInfo = new PageInfo<>(list);
        model.addAttribute("PageInfo", pageInfo);
        return "supplier/car/carSystem";
    }

    //查看车辆销售详情---详情页
    @RequestMapping("/selects")
    String shows(Model model, @RequestParam("brand") String brand, @RequestParam("type") String type) {
        model.addAttribute("shows", carService.selects(brand, type));
        return "supplier/car/xiangxi";
    }

    @RequestMapping("/selectCars")
    String selectCars(Model model) {
        List<Car> carList = carService.queryAll();
        int sum1 = 0;
        int sum2 = 0;
        int sum3 = 0;
        int sum4 = 0;
        int sum5 = 0;
        int sum6 = 0;
        int sum7 = 0;
        int sum8 = 0;
        int sum9 = 0;
        int sum10 = 0;
        int sum11 = 0;
        int sum12 = 0;

        for (int i = 0; i < carList.size(); i++) {
            //判断季度
            Date da = carList.get(i).getCarInDate();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(da);
            int month = calendar.get(calendar.MONTH);
            int quarter = 0;
            if (month >= 1 && month <= 3) {
                quarter = 1;
            } else if (month >= 4 && month <= 6) {
                quarter = 2;
            } else if (month >= 7 && month <= 9) {
                quarter = 3;
            } else {
                quarter = 4;
            }

            //每个季度的车的订单---车辆入库柱形图
            if (quarter == 1) {
                if (carList.get(i).getCarVar().equals("日常")) {
                    sum1 += carList.get(i).getCarTotal();
                } else if (carList.get(i).getCarVar().equals("服装")) {
                    sum2 += carList.get(i).getCarTotal();
                } else if (carList.get(i).getCarVar().equals("电器")) {
                    sum3 += carList.get(i).getCarTotal();
                }
            } else if (quarter == 2) {
                if (carList.get(i).getCarVar().equals("日常")) {
                    sum4 += carList.get(i).getCarTotal();
                } else if (carList.get(i).getCarVar().equals("服装")) {
                    sum5 += carList.get(i).getCarTotal();
                } else if (carList.get(i).getCarVar().equals("电器")) {
                    sum6 += carList.get(i).getCarTotal();
                }
            } else if (quarter == 3) {
                if (carList.get(i).getCarVar().equals("日常")) {
                    sum7 += carList.get(i).getCarTotal();
                } else if (carList.get(i).getCarVar().equals("服装")) {
                    sum8 += carList.get(i).getCarTotal();
                } else if (carList.get(i).getCarVar().equals("电器")) {
                    sum9 += carList.get(i).getCarTotal();
                } else if (quarter == 4) {
                    if (carList.get(i).getCarVar().equals("日常")) {
                        sum10 += carList.get(i).getCarTotal();
                    } else if (carList.get(i).getCarVar().equals("服装")) {
                        sum11 += carList.get(i).getCarTotal();
                    } else if (carList.get(i).getCarVar().equals("电器")) {
                        sum12 += carList.get(i).getCarTotal();
                    }
                }
            }
        }
        model.addAttribute("sum1", sum1);
        model.addAttribute("sum2", sum2);
        model.addAttribute("sum3", sum3);
        model.addAttribute("sum4", sum4);
        model.addAttribute("sum5", sum5);
        model.addAttribute("sum6", sum6);
        model.addAttribute("sum7", sum7);
        model.addAttribute("sum8", sum8);
        model.addAttribute("sum9", sum9);
        model.addAttribute("sum10", sum10);
        model.addAttribute("sum11", sum11);
        model.addAttribute("sum12", sum12);
        return "supplier/car/selectCars";
    }
}
